<template>
<div>
    <div style="margin: 10px 0; ">
        <el-input  placeholder="请输入学号" style="width: 400px;" suffix-icon="el-icon-search
" v-model="studentId">
    </el-input ><el-button  class="ml-5" type="primary" @click="load" icon="el-icon-search">搜索</el-button>
    <el-button  class="ml-5" type="primary" @click="reset" icon="el-icon-refresh" >重置</el-button>
    </div>
    <div style="margin: 10px 0; ">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-button type="primary" @click="delSs" >批量删除<i class="el-icon-delete"></i></el-button>
    </div>

    <el-table :data="tableData"  stripe :header-cell-class-name="headerBg" border @selection-change="handleSelectionChange">
      <el-table-column
      type="selection"
      width="55">
    </el-table-column>
      <el-table-column prop="studentId" label="学号" width="200">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="150">
        </el-table-column>
        <el-table-column prop="enrollmentYear" label="入学年份" width="150">
        </el-table-column>
        <el-table-column prop="department" label="专业" width="150">
        </el-table-column>
        <el-table-column prop="grade" label="年级" width="150">
        </el-table-column>
        <el-table-column prop="contactInfo" label="联系方式" width="250">
        </el-table-column>
          <el-table-column>
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
              <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="你确定删除该生信息吗"
              @confirm="del(scope.row.studentId)"
              >
              <el-button type="primary" icon="el-icon-delete" slot="reference"></el-button></el-popconfirm>
            </template>
          </el-table-column>
    </el-table>
    <div class="block" style="padding: 10px;">
    
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
   <!-- 弹窗 -->
  <el-dialog title="学生信息" :visible.sync="dialogFormVisible" >
  <el-form :model="form" label-width="100px">
    <el-form-item label="学号" >
      <el-input v-model="form.studentId" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="姓名" >
      <el-input v-model="form.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="入学年份" >
      <el-input v-model="form.enrollmentYear" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="专业" >
      <el-input v-model="form.department" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="年级" >
      <el-input v-model="form.grade" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="联系方式" >
      <el-input v-model="form.contactInfo" autocomplete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    
    <el-button type="primary" @click="save">确 定</el-button>
  </div>
</el-dialog>
</div>
</template>


<script>

import request from '../../utils/request';



export default{
    name: 'Suser',
    data(){
      
  return{
    tableData:[],
    total: 0,
    pageNum:1,
    pageSize: 5,
    studentId:'',
    headerBg:'headerBg',
    multipleSelection: [],
    dialogFormVisible: false,
    form:{
      studentId: '',
      name: '',
      enrollmentYear: '',
      department: '',
      grade: '',
      contactInfo: '',

    }
  }
  },
  created(){
    this.load()
  },
  methods: {
  load(){
    request.get("/Admin/suser",{
      params:{
        pageNum : this.pageNum,
        pageSize: this.pageSize,
        studentId: this.studentId
        
      }
    }).then(res => {
      console.log(res)
      this.tableData = res.data.records
      this.total = res.data.total
    })
  },
  handleSizeChange(pageSize) {
    console.log(pageSize)
    this.pageSize = pageSize
    this.load()
   
  },
  handleCurrentChange(pageNum) {
    console.log(pageNum)
    this.pageNum = pageNum
   this.load()
  },
  // 重置按钮
  reset(){
    this.studentId=''
    this.load()
  },
  handleAdd(){
    this.dialogFormVisible = true
    this.form = {}
  },
  //新增
  save(){
    request.post('/Admin/saveS',this.form).then(res =>{
      if(res.data.code == "0"){
        this.$message({
            message: res.data.msg,
            type: 'success'
           }),
        this.dialogFormVisible =  false
        this.load()
      }else{
        this.$message.error({
            
            message: res.data.msg,
            
           }),
        this.dialogFormVisible= false
      }
    })
  },
  //编辑
  handleEdit(row){
    this.form = row
    this.dialogFormVisible = true
  },
  //删除
  del(studentId){
    request.get("/Admin/delS/"+studentId).then(res =>{
      if(res.data.code == "0"){
        this.$message({
            message: res.data.msg,
            type: 'success'
           }),
        this.dialogFormVisible =  false
        this.load()
      }else{
        this.$message.error({
            
            message: res.data.msg,
            
           }),
        this.dialogFormVisible= false
      }
    })
  },
  delSs(){
    let  ids = this.multipleSelection.map(v => v.studentId)
    request.post("/Admin/delSs",ids).then(res =>{
      if(res.data.code == "0"){
        this.$message({
            message: res.data.msg,
            type: 'success'
           }),
        this.dialogFormVisible =  false
        this.load()
      }else{
        this.$message.error({
            
            message: res.data.msg,
            
           }),
        this.dialogFormVisible= false
      }
    })
  },
  handleSelectionChange(val){
    this.multipleSelection = val
  }
 }
}
 
</script>

<style scoped>
.headerBg{
  background-color: #bcb7b7!important;
}
</style>